/*
 * timers_hw.c
 *
 *  Created on: Sep 14, 2013
 *      Author: Ken Arok
 */


#include "timers_hw.h"


volatile uint32_t TimersArray500ms[16];
volatile uint32_t TimersArray10ms[16];
TimerFuncVoid_t FuncArray500ms[16] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
									NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
TimerFuncVoid_t FuncArray10ms[16] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
									NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
void *ParamFuncArray500ms[16];
void *ParamFuncArray10ms[16];

/* Timer mask bit. */
static uint16_t func500MaskBit = 0x0000;
static uint16_t func10MaskBit = 0x0000;
static uint16_t tmr500MaskBit = 0x0000;
static uint16_t tmr10MaskBit = 0x0000;


void vTimer500msFuncReg(TimerFuncVoid_t _func, void *_param)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(!(func500MaskBit & _mask)) {
			func500MaskBit |= _mask ;
			FuncArray500ms[i] = _func;
			ParamFuncArray500ms[i] = _param;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}


void vTimer500msFuncUnreg(TimerFuncVoid_t _func)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(_func == FuncArray500ms[i]) {
			func500MaskBit &= ~_mask;
			FuncArray500ms[i] = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}


void vTimer10msFuncReg(TimerFuncVoid_t _func, void *_param)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(!(func10MaskBit & _mask)) {
			func10MaskBit |= _mask ;
			FuncArray10ms[i] = _func;
			ParamFuncArray10ms[i] = _param;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}

void vTimer10msFuncUnreg(TimerFuncVoid_t _func)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(_func == FuncArray10ms[i]) {
			func10MaskBit &= ~_mask;
			FuncArray10ms[i] = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}


volatile void *vTimer10msTake(void)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(!(tmr10MaskBit & _mask)) {
			tmr10MaskBit |= _mask ;
			_exit = 0;
			return &TimersArray10ms[i];
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));

	return NULL;
}


void vTimer10msGive(volatile void *_tmrAddr)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(_tmrAddr == &TimersArray10ms[i]) {
			tmr10MaskBit &= ~_mask;
			_tmrAddr = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}


volatile void *vTimer500msTake(void)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(!(tmr500MaskBit & _mask)) {
			tmr500MaskBit |= _mask ;
			_exit = 0;
			return &TimersArray500ms[i];
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));

	return NULL;
}



void vTimer500msGive(volatile void *_tmrAddr)
{
	uint8_t i = 0, _exit = 1;
	uint16_t _mask = 0x0001;

	do {
		if(_tmrAddr == &TimersArray500ms[i]) {
			tmr500MaskBit &= ~_mask;
			_tmrAddr = NULL;
			_exit = 0;
		}
		_mask = (_mask << 1);
		i++;
	}while(_exit && (i < 16));
}
